package com.rengl.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class MyFilter extends ZuulFilter {

    //日志对象
    private static Logger log = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤
     * 1.获得请求
     * 2.取出令牌
     * 3.验证令牌
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {

        //1.获取请求上下文
        RequestContext ctx = RequestContext.getCurrentContext();

        //2.获取请求
        HttpServletRequest request = ctx.getRequest();

        //3.获取令牌
        String token = request.getParameter("token");

        //验证令牌
        if(token == null){

            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            return null;
        }


        return null;
    }
}
